package cz.zcu.fav.rat.managed.bean;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import cz.zcu.fav.rat.beans.Object;
import cz.zcu.fav.rat.beans.User;
import cz.zcu.fav.rat.dataaccess.service.UserService;
import cz.zcu.fav.rat.security.beans.UserSessionController;

@ManagedBean(name = "favouritesTableBean")
@ViewScoped
public class FavouritesTableBean extends AbstractTableBean<Object, UserService>
{
	private static final long serialVersionUID = 4603058317639872721L;
	private Integer loggedUserId;

	@PostConstruct
	public void init()
	{
		loggedUserId = UserSessionController.getLoggedUserBean().getId();
	}


	@Override
	protected void performDelete(List<Object> objects)
	{
		User user = getDataProvider().get(loggedUserId);
		Set<Object> favourites = user.getFavourites();
		favourites.removeAll(objects);
		user.setFavourites(favourites);
		getDataProvider().update(user);
	}

	@Override
	protected List<Object> getData()
	{
		List<Object> list = new ArrayList<Object>();
		list.addAll(getDataProvider().get(loggedUserId).getFavourites());
		return list;
	}

	@Override
	protected UserService getService()
	{
		return SpringBeanFactory.getInstance().getBean("userService",
				UserService.class);
	}

}
